﻿#include "showresultdialog.h"
#include "ui_showresultdialog.h"

#include "toast.h"
#define MAX_SHOW_LINES 100
ShowResultDialog::ShowResultDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ShowResultDialog)
{
    ui->setupUi(this);
    setWindowFlags(Qt::WindowCloseButtonHint | Qt::MSWindowsFixedSizeDialogHint | Qt::Dialog);

    ui->horizontalLayout_2->insertStretch(0);
    QIntValidator* aIntValidator = new QIntValidator;
    ui->lineEdit->setValidator(aIntValidator);

    QStringList header;
    header<<tr("Date")<<tr("Time")<<"Result";
    ui->tableWidget->setHorizontalHeaderLabels(header);
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); //整行选中的方式
    QHeaderView * h = ui->tableWidget->horizontalHeader();
    h->setSectionResizeMode(QHeaderView::Stretch);
    this->setLayout(ui->verticalLayout);

//    for (int i=0; i<3; i++)
//    {
//        QTableWidgetItem *columnHeaderItem0 = ui->tableWidget->horizontalHeaderItem(i); //获得水平方向表头的Item对象
//        columnHeaderItem0->setFont(QFont("微软雅黑", 13)); //设置字体
////        columnHeaderItem0->setBackgroundColor(QColor(0,60,10*i)); //设置单元格背景颜色
////        columnHeaderItem0->setTextColor(QColor(200,111,30*i)); //设置文字颜色
//    }
    m_page = 1;

    connect(ui->pushButton_Next, &QPushButton::clicked, [=](){
        double total = m_res.size();
        int pages = static_cast<int>(ceil(total / MAX_SHOW_LINES));
        if (pages != m_page)
        {
            m_page++;
            JumpToPage(m_page);
        }
        else{
            QString tip = tr("Have reached the last page");
            Toast *w = new Toast(this, tip, 280, 30);
            w->toast();
        }

    });

    connect(ui->pushButton_Pre, &QPushButton::clicked, [=](){
        if (1 != m_page)
        {
            m_page--;
            JumpToPage(m_page);
        }
        else{
            QString tip = tr("Have reached the first page");
            Toast *w = new Toast(this, tip, 280, 30);
            w->toast();
        }
    });

    connect(ui->pushButton_First, &QPushButton::clicked, [=](){
        if (1 != m_page)
        {
            m_page = 1;
            JumpToPage(m_page);
        }
        else{
            QString tip = tr("Have reached the first page");
            Toast *w = new Toast(this, tip, 280, 30);
            w->toast();
        }
    });

    connect(ui->pushButton_Last, &QPushButton::clicked, [=](){
        double total = m_res.size();
        int pages = static_cast<int>(ceil(total / MAX_SHOW_LINES));
        if (pages != m_page)
        {
            m_page = pages;
            JumpToPage(m_page);
        }
        else{
            QString tip = tr("Have reached the last page");
            Toast *w = new Toast(this, tip, 280, 30);
            w->toast();
        }
    });

    connect(ui->lineEdit, &QLineEdit::editingFinished, [=]{
        double total = m_res.size();
        int pages = static_cast<int>(ceil(total / MAX_SHOW_LINES));
        int page = ui->lineEdit->text().toInt();

        if (page > pages || page < 1)
        {
            ui->lineEdit->setText(QString::number(m_page));

            QString tip = tr("Error page number");
            Toast *w = new Toast(this, tip, 280, 30);
            w->toast();

        }
        else {
            m_page =  page;
            JumpToPage(m_page);
        }
    });

    connect(ui->pushButton_Jump, &QPushButton::clicked, [=](){
        double total = m_res.size();
        int pages = static_cast<int>(ceil(total / MAX_SHOW_LINES));
        int page = ui->lineEdit->text().toInt();

        if (page > pages || page < 1)
        {
            ui->lineEdit->setText(QString::number(m_page));
        }
        else {
            m_page =  page;
            JumpToPage(m_page);
        }
    });
}

ShowResultDialog::~ShowResultDialog()
{
    delete ui;
}

void ShowResultDialog::SetReslut(QVector<ResultManager::RESULT_DEF> v)
{
    m_res = v;
    double total = m_res.size();
    int pages = static_cast<int>(ceil(total / MAX_SHOW_LINES));
    ui->labelNum->setText(QString::number(pages));
    JumpToPage(m_page);
}

void ShowResultDialog::JumpToPage(int page)
{

    double total = m_res.size();
    int pages = static_cast<int>(ceil(total / MAX_SHOW_LINES));
    if(page > pages || page < 1)
    {
        return;
    }

    ui->tableWidget->setRowCount(0);
    ui->tableWidget->clearContents();
    for (int i=(page-1)*MAX_SHOW_LINES; i<page*MAX_SHOW_LINES&&i<total; i++)
    {
        int count = ui->tableWidget->rowCount();
        ui->tableWidget->insertRow(count);
        ResultManager::RESULT_DEF res = m_res.at(i);
        QDateTime time = res.time;
        {
            QString str = QString("%1").arg(time.toString("yyyy-MM-dd"));
            QTableWidgetItem *item = new QTableWidgetItem(str);
            item->setTextAlignment(Qt::AlignCenter);
//            item->setTextColor(QColor(200,111,100));
            item->setFont(QFont("Helvetica", 12));

            ui->tableWidget->setItem(count, 0, item);
        }
        {
            QString str = QString("%1").arg(time.toString("hh:mm:ss"));
            QTableWidgetItem *item = new QTableWidgetItem(str);
            item->setTextAlignment(Qt::AlignCenter);
//            item->setTextColor(QColor(200,111,100));
            item->setFont(QFont("Helvetica", 12));

            ui->tableWidget->setItem(count, 1, item);
        }

      
        int r = res.result;
        QTableWidgetItem *item = new QTableWidgetItem("Pass");
            item->setTextColor(QColor(255, 255, 255));
        item->setBackgroundColor(QColor(0, 200, 0));
        if (r==0)
        {
            item->setText("Fail");
            item->setBackgroundColor(QColor(200, 0, 0));
        }
        item->setTextAlignment(Qt::AlignCenter);
    //    item->setTextColor(QColor(200,111,100));
        item->setFont(QFont("Helvetica", 20, QFont::Bold));

        ui->tableWidget->setItem(count, 2, item);

        
    }
    ui->lineEdit->setText(QString::number(m_page));


}
